<template>
  <view class="normal-login-container">
    <view class="item-wrap">
      <view class="lineBottom10">
        <uni-section title="处置结果" type="line"/>
      </view>

      <view style="margin: 8rpx 20rpx 8rpx 20rpx">
        <uni-forms ref="ruleForm" :model="ruleForm" labelWidth="auto" :label-position="boole ? 'top' : 'left'">


          <uni-forms-item label="初期残值(元):">
            <uni-easyinput v-model="ruleForm.initialResidualValue" v-if="boole" disabled placeholder="初期残值(元):"/>
            <text v-else>{{ ruleForm.initialResidualValue || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="最新残值(元):">
            <uni-easyinput v-model="ruleForm.latestResidualValue" v-if="boole" disabled placeholder="最新残值(元):"/>
            <text v-else>{{ ruleForm.latestResidualValue || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="承销商:" name="underwriterType" :required="boole"
                          :rules="[{required:true, errorMessage: '请选择承销商',validateTrigger:'change'}]">
            <uni-data-picker placeholder="请选择承销商" :localdata="options"
                             ref="picker" v-if="boole"
                             v-model="ruleForm.underwriterType"
                             @change="onchange">
            </uni-data-picker>
            <text v-else>{{ ruleForm.underwriterName || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="成交价(元):" name="finalPrice" :required="boole"
                          :rules="[{required:true, errorMessage: '成交价(元)',validateTrigger:'bind'}]">
            <text class="finalPrice"/>
            <uni-easyinput v-model="ruleForm.finalPrice" v-if="boole"
                           @change="handleChange"
                           placeholder="成交价(元):"/>
            <text v-else>{{ ruleForm.finalPrice || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="差额(元):">
            <uni-easyinput v-model="ruleForm.priceDifference" v-if="boole" disabled placeholder="申请修改残值(元):"/>
            <text v-else>{{ ruleForm.priceDifference || '-' }}</text>
          </uni-forms-item>

          <uni-forms-item label="说明:">
            <text class="specification"/>
            <uni-easyinput type="textarea" v-if="boole" :maxlength="500" v-model="ruleForm.routineRemark" placeholder="说明"/>
            <text v-else>{{ ruleForm.routineRemark || '-' }}</text>
          </uni-forms-item>


          <uni-forms-item label="附件:">
            <image-common :disabled="!boole" :up-type="2" v-model="ruleForm.routineAttachmentOss"/>
          </uni-forms-item>
        </uni-forms>
      </view>
    </view>

  </view>
</template>

<script>
import * as apis from '@/api/routineHandle'
import {getSupplierListC} from "@/api/routineHandle";
import {toNumberSub} from '@/pages/quota/libs/utils.js';
export default {
  name: 'disposalResult',
  dicts: ['supplier_business_type'],
  props: {
    value: {
      type: Object,
      default: {},
    },
    boole: {
      type: Boolean,
      default: true,
    }
  },
  computed: {
    ruleForm: {
      get() {
        let that = this
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key)
          },
          set(target, key, value) {
            that.$emit('input', {...target, [key]: value})
            return true
          },
        })
        return proxy
      },
    },
    handleLabel() {
      return function (arr, type) {
        let name = ''
        arr.map((v) => {
          if (type == v.value) {
            name = v.label
          }
        })
        return name
      }
    },

  },
  watch: {},
  data() {
    return {
      options: [],
      supplierListL: []
    }
  },
  created() {

  },
  mounted() {
    setTimeout(() => {
      this.optionsList()
    }, 300)
  },
  methods: {
    optionsList() {
      let val = this.dict.type.supplier_business_type
      this.options = Array.from(val, item => ({
        value: item.value,
        text: item.label,
        children: []
      }))
      if (this.value.underwriterType && this.value.underwriterType.length === 2) {
        this.handleSecond()
      }
    },

    async handleSecond() {
      let index = this.options.findIndex(a => a.value === this.value.underwriterType[0])
      try {
        let res = await apis?.getSupplierListC({supplierTypeCode: this.value.underwriterType[0]})
        this.supplierListL = res.data || []
        let children = Array.from(this.supplierListL, item => ({
          value: item.supplierId,
          text: item.supplierName,
        }))
        if (index !== -1) {
          this.$set(this.options[index], 'children', children)
        }
      } catch (e) {
        console.error(e)
      }

    },

    async onchange(e) {
      try {
        let key = e.detail.value[0].value
        let index = this.options.findIndex(a => a.value === key)
        let res = await apis?.getSupplierListC({supplierTypeCode: key})
        this.supplierListL = res.data || []
        let children = Array.from(this.supplierListL, item => ({
          value: item.supplierId,
          text: item.supplierName,
        }))
        if (index !== -1) {
          this.$set(this.options[index], 'children', children)
          this.$nextTick(() => {
            this.$refs.picker.show()
          });
          this.$nextTick(() => {
            if (e.detail.value.length === 2) {
              this.$refs.picker.hide()
            }
          });
        }
        console.log(e.detail.value)
        this.ruleForm.underwriterType = this.value.underwriterType = [e.detail.value[0].value, e.detail.value[1].value]
        this.handleUnderwriterName()
      } catch (e) {
        console.error(e)
      }
    },

    handleUnderwriterName() {
      let obj = this.supplierListL.find(item => item.supplierId === this.ruleForm.underwriterType[1])
      this.value.underwriterId = this.ruleForm.underwriterId =this.ruleForm.underwriterType[1]
      this.value.underwriterName = this.ruleForm.underwriterName = obj.supplierName
    },

    handleChange(val) {
      let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
      if (!msg.test(val)) {
        this.ruleForm.finalPrice = this.value.finalPrice = "";
      } else {
        this.ruleForm.finalPrice = this.value.finalPrice = val
            .toString()
            .match(/^\d+(?:\.\d{0,2})?/)[0];
      }
      this.ruleForm.priceDifference = toNumberSub(Number(this.ruleForm.finalPrice), Number(this.ruleForm.latestResidualValue))
    },

    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm
            .validate()
            .then((res) => {
              resolve()
            })
            .catch((err) => {
              let key = '.flex-col' + '>>>' + '.' + err[0].key
              reject(key)
            })
      })
    },
  },
}
</script>

<style scoped lang="scss">
</style>


